import request from '../utils/request';

/**
 * 创建路由规则
 * @param {string} type
 * @param {string} name
 * @param {string} description
 * @param {boolean} active
 */
export function createRouterApi(type, name, description, active) {
  return request({
    url: '/routers',
    method: 'post',
    data: Object.assign(
      {
        type,
        name,
        active
      },
      description ? { description } : null
    )
  });
}

/**
 * 删除路由规则
 * @param {string} uuid
 */
export function deleteRouterApi(uuid) {
  return request({
    url: '/routers/' + uuid,
    method: 'delete'
  });
}

/**
 * 更新路由规则
 * @param {string} uuid
 * @param {string} type
 * @param {string} name
 * @param {string} description
 * @param {boolean} active
 */
export function updateRouterApi(uuid, type, name, description, active) {
  return request({
    url: '/routers/' + uuid,
    method: 'put',
    data: {
      type,
      name,
      description: description || null,
      active
    }
  });
}

/**
 * 获取路由规则列表
 * @param {number} pageIndex
 * @param {number} pageSize
 * @param {string} sortType
 * @param {string} orderBy
 * @param {string} type
 * @param {string} name
 * @param {string} description
 * @param {boolean} active
 */
export function getRouterListApi(pageIndex, pageSize, sortType, orderBy, type, name, description, active) {
  return request({
    url: '/routers',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType,
        order_by: orderBy
      },
      type ? { type } : null,
      name ? { name } : null,
      description ? { description } : null,
      typeof active === 'boolean' ? { active: active } : null
    )
  });
}

/**
 * 更新路由规则下的设备列表
 * @param {string} uuid
 * @param {[string]} senderDeviceUuidList
 * @param {[string]} receiverDeviceUuidList
 */
export function updateRouterDeviceListApi(uuid, senderDeviceUuidList, receiverDeviceUuidList) {
  return request({
    url: '/routers/' + uuid + '/devices',
    method: 'put',
    data: {
      sender_device_uuid_list: senderDeviceUuidList,
      receiver_device_uuid_list: receiverDeviceUuidList
    }
  });
}
